package com.food.user.controller;

import com.food.user.commos.Result;
import com.food.user.dto.CartReturnDto;
import com.food.user.exception.MyParameterException;
import com.food.user.form.AddCartForm;
import com.food.user.form.UpdateCartCountForm;
import com.food.user.service.CartService;
import com.food.user.util.JwtUtil;
import com.food.user.util.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/cart")
@Api(tags = "购物车相关接口")
public class CartController {

    @Autowired
    private CartService cartService;

    @PostMapping("/add")
    @ApiOperation("新增购物车")
    public Result add(@Valid @RequestBody AddCartForm form, BindingResult br) throws Exception{
        if (br.hasErrors()) {
            List<ObjectError> allErrors = br.getAllErrors();
            for (ObjectError allError : allErrors) {
                //获取打印提示信息
                System.out.println(allError.getDefaultMessage());
            }
            throw new MyParameterException(500, "参数校验失败");
        }
        System.out.println(form);
        //获取用户id
        String accessToken = WebUtil.getRequest().getHeader("accessToken");
        Integer userId = JwtUtil.getUserId(accessToken);
        cartService.add(form,userId);
        return Result.success(200,"新增购物车成功",null);
    }

    @PostMapping("/updateCount")
    @ApiOperation("修改购物车数量")
    public Result updateCount(@RequestBody UpdateCartCountForm form, BindingResult br) throws Exception{
        if (br.hasErrors()) {
            List<ObjectError> allErrors = br.getAllErrors();
            for (ObjectError allError : allErrors) {
                //获取打印提示信息
                System.out.println(allError.getDefaultMessage());
            }
            throw new MyParameterException(500, "参数校验失败");
        }
        //获取用户id
        String accessToken = WebUtil.getRequest().getHeader("accessToken");
        Integer userId = JwtUtil.getUserId(accessToken);
        cartService.update(form,userId);
        return Result.success(200,"修改购物车菜品的数量成功",null);
    }

    @PostMapping("/del")
    @ApiOperation("删除购物车")
    public Result del(Long foodInfoId)throws Exception{
        //获取用户id
        String accessToken = WebUtil.getRequest().getHeader("accessToken");
        Integer userId = JwtUtil.getUserId(accessToken);
        if (cartService.delete(foodInfoId, userId)) {
            return Result.success(200,"删除购物车成功",null);
        }
        return Result.success(200,"购物车没有该数据",null);

    }

    @GetMapping("/showMyCart")
    @ApiOperation("查询我的购物车")
    public Result showMyCart() throws Exception{
        //获取用户id
        String accessToken = WebUtil.getRequest().getHeader("accessToken");
        Integer userId = JwtUtil.getUserId(accessToken);
        List<CartReturnDto> list = cartService.showMyCart(userId);
        return Result.success(200,"查询我的购物车成功",list);
    }


}
